<template>
    <fieldset>
        <legend>登录页</legend>
        用户名:
        <input type="text" v-model="username">
        <br />
        密码:
        <input type="text" v-model="password" @keydown.enter="login">
        <button @click="login">登录</button>
    </fieldset>
</template>
<script>
import * as api from "@/api"
import { mapMutations } from 'vuex'
export default {
    data() {
        return {
            username: '',
            password: ''
        }
    },
    methods: {
        ...mapMutations(['SET_USERINFO']),
        login() {
            let params = {
                username: this.username,
                password: this.password
            }
            api.loginApi(params)
                .then(res => {
                    // console.log(res)
                    this.SET_USERINFO(res.userInfo)
                    if (res.state) {
                        this.$router.push('/home')
                    }else{
                        alert(res.msg)
                    }
                })
        }

    }
}
</script>